#!groovy

node {
  def rootPath = "/src/rancher-validation/"
  def setupContainer = "${JOB_NAME}${env.BUILD_NUMBER}_setup"
  def testContainer = "${JOB_NAME}${env.BUILD_NUMBER}_test"

  def deployPytestOptions = "-k test_deploy_rancher_server"

  def setupResultsOut = "setup-results.xml"
  def testResultsOut = "results.xml"
  def imageName = "rancher-validation-${JOB_NAME}${env.BUILD_NUMBER}"
  def testsDir = "tests/v3_api/"

  def envFile = ".env"
  def rancherConfig = "rancher_env.config"

  def branch = "master"
  if ("${env.branch}" != "null" && "${env.branch}" != "") {
    branch = "${env.branch}"
  }

  wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm', 'defaultFg': 2, 'defaultBg':1]) {
    stage('Checkout') {
      deleteDir()
      checkout([
                $class: 'GitSCM',
                branches: [[name: "*/${branch}"]],
                extensions: scm.extensions + [[$class: 'CleanCheckout']],
                userRemoteConfigs: scm.userRemoteConfigs
              ])
    }

    dir ("tests/validation") {
      stage('Configure and Build') {
        if (env.AWS_SSH_PEM_KEY && env.AWS_SSH_KEY_NAME) {
          dir(".ssh") {
            def decoded = new String(AWS_SSH_PEM_KEY.decodeBase64())
            writeFile file: AWS_SSH_KEY_NAME, text: decoded
          }
        }
    
        sh "./tests/v3_api/scripts/configure.sh"
        sh "./tests/v3_api/scripts/build.sh"
      }
      try {
        stage('Run Validation Tests') {
          try {
            // deploy rancher server
            sh "docker run --name ${setupContainer} -t --env-file ${envFile} " +
              "${imageName} /bin/bash -c \'pytest -v -s --junit-xml=${setupResultsOut} " +
              "${deployPytestOptions} ${testsDir}\'"

            // copy file containing CATTLE_TEST_URL & ADMIN_TOKEN and load into environment variables
            sh "docker cp ${setupContainer}:${rootPath}${testsDir}${rancherConfig} ."
            load rancherConfig

            // run tests
            sh "docker run --name ${testContainer} -t --env-file ${envFile} " +
              "${imageName} /bin/bash -c \'export CATTLE_TEST_URL=${env.CATTLE_TEST_URL} " +
              "&& export ADMIN_TOKEN=${env.ADMIN_TOKEN} && pytest -v -s --junit-xml=${testResultsOut} " +
              "${PYTEST_OPTIONS} ${testsDir}\'"
          } catch(err) {
            echo "Error: " + err
            echo 'Test run had failures. Collecting results...'
          }
        }

        stage('Test Report') {
          // copy and archive test results
          sh "docker cp ${setupContainer}:${rootPath}${setupResultsOut} ."
          sh "docker cp ${testContainer}:${rootPath}${testResultsOut} ."

          step([$class: 'JUnitResultArchiver', testResults: "**/${setupResultsOut}"])
          step([$class: 'JUnitResultArchiver', testResults: "**/${testResultsOut}"])

          sh "docker rm -v ${setupContainer}"
          sh "docker rm -v ${testContainer}"
          sh "docker rmi ${imageName}"
        }
      } catch(err){
        sh "docker stop ${setupContainer}"
        sh "docker stop ${testContainer}"

        sh "docker rm -v ${setupContainer}"
        sh "docker rm -v ${testContainer}"
        
        sh "docker rmi ${imageName}"
      }
    }
  }
}